package com.acwing.partition2;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/3 9:16
 */
public class AC104货仓选址 {

    private static final int N = 100010;
    private static int[] w = new int[N];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(ss[i - 1]);
        Arrays.sort(w, 1, n + 1);
        int ans = 0;
        for (int i = 1; i <= n; i++) ans += Math.abs(w[i] - w[(n + 1) / 2]);
        writer.write(ans + "\n");
        writer.flush();
    }
}
